"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 行存压缩
Case Name   : 普通非压缩表，修改compresstype=2,compress_chunk_size为2048
            及compress_prealloc_chunks不同取值，部分合理报错
Create At   : 2023/8/2
Owner       : @li-xin12345
Description :
    1、建普通表并插入数据
    2、修改压缩参数compresstype=2,compress_chunk_size 2048,
    compress_prealloc_chunks为0到3
    3、修改压缩参数compresstype=2,compress_chunk_size 2048,
    compress_prealloc_chunks为4到7
    4、修改压缩参数compresstype=2,compress_chunk_size 2048,
    compress_prealloc_chunks取值范围外
    5、修改压缩参数compresstype=2,compress_chunk_size 2048,
    compress_prealloc_chunks无效值
    6、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、合理报错
    5、合理报错
    6、成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class RowCompressTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.tb_name = 't_alter_rowcompress_0014'

    def test_row_compress(self):
        text = '-----step1:建普通表并插入数据;expect:成功-----'
        self.log.info(text)
        sql = f'''drop table if exists {self.tb_name} cascade;
        create table {self.tb_name}(columnone integer ,
        columntwo char(50) ,columnthree varchar(50) ,columnfour integer ,
        columnfive char(50) ,columnsix varchar(50) ,columnseven char(50) ,
        columneight char(50) ,columnnine varchar(50) ,columnten varchar(50) ,
        columneleven char(50) ,columntwelve char(50) ,
        columnthirteen varchar(50) ,columnfourteen char(50) ,
        columnfifteem varchar(50)); 
        insert into {self.tb_name} values(generate_series(1, 1000),
        'test','abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx',
         2, 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx', 
         'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx', 
         'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx', 
         'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx', 
         'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx', 
         'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx', 
         'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx', 
         'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx', 
         'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx', 
         'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx', 
         'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx');
        '''
        sql_res = self.pri_sh.execut_db_sql(sql)
        self.log.info(sql_res)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, sql_res,
                      '执行失败：' + text)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, sql_res,
                      '执行失败：' + text)
        self.assertIn('INSERT 0 1000', sql_res, '执行失败：' + text)

        text = '-----step2:修改压缩参数compresstype=2,' \
               'compress_chunk_size 2048,compress_prealloc_chunks为0到3;' \
               'expect:成功-----'
        self.log.info(text)
        for i in range(0, 4):
            sql = f'alter table {self.tb_name} set (compresstype=2, ' \
                  f'compress_chunk_size=2048, compress_prealloc_chunks={i});' \
                  f'checkpoint;' \
                  f'\\d+ {self.tb_name}'
            sql_res = self.pri_sh.execut_db_sql(sql)
            self.log.info(sql_res)
            self.assertIn(self.constant.ALTER_TABLE_MSG, sql_res,
                          '执行失败：' + text)
            self.assertIn(self.constant.CHECKPOINT_SUCCESS_MSG, sql_res,
                          '执行失败：' + text)
            self.assertIn(f'compresstype=2, compress_chunk_size=2048, '
                          f'compress_prealloc_chunks={i}', sql_res,
                          '执行失败：' + text)
            i += 1

        text = '-----step3:修改压缩参数compresstype=2,' \
               'compress_chunk_size 2048,compress_prealloc_chunks为4到7;' \
               'expect:合理报错-----'
        self.log.info(text)
        for i in range(4, 8):
            sql = f'alter table {self.tb_name} set (compresstype=2, ' \
                  f'compress_chunk_size=2048, compress_prealloc_chunks={i});'
            sql_res = self.pri_sh.execut_db_sql(sql)
            self.log.info(sql_res)
            self.assertIn(f'ERROR:  invalid compress_prealloc_chunks',
                          sql_res, '执行失败：' + text)
            i += 1

        text = '-----step4:修改压缩参数compresstype=2,' \
               'compress_chunk_size 2048,' \
               'compress_prealloc_chunks取值范围外;expect:合理报错-----'
        self.log.info(text)
        sql = f'alter table {self.tb_name} set (compresstype=2, ' \
              f'compress_chunk_size=2048, compress_prealloc_chunks=-1);' \
              f'alter table {self.tb_name} set (compresstype=2, ' \
              f'compress_chunk_size=2048, compress_prealloc_chunks=8);'
        sql_res = self.pri_sh.execut_db_sql(sql)
        self.log.info(sql_res)
        self.assertIn(f'DETAIL:  Valid values are between "0" and "7"',
                      sql_res, '执行失败：' + text)
        self.assertEqual(sql_res.count('ERROR'), 2, '执行失败：' + text)

        text = '-----step5:修改压缩参数compresstype=2,' \
               'compress_chunk_size 2048,' \
               'compress_prealloc_chunks无效值;expect:合理报错-----'
        self.log.info(text)
        sql = f'''alter table {self.tb_name} set (compresstype=2, 
        compress_chunk_size=2048, compress_prealloc_chunks='test');
        alter table {self.tb_name} set (compresstype=2, 
        compress_chunk_size=2048, compress_prealloc_chunks=2.2);
        '''
        sql_res = self.pri_sh.execut_db_sql(sql)
        self.log.info(sql_res)
        self.assertIn(f'ERROR:  invalid value for integer option '
                      f'"compress_prealloc_chunks"',
                      sql_res, '执行失败：' + text)
        self.assertEqual(sql_res.count('ERROR'), 2, '执行失败：' + text)

    def tearDown(self):
        text = '-----step6:清理环境; expect:成功-----'
        self.log.info(text)
        sql = f'drop table if exists {self.tb_name};'
        drop_res = self.pri_sh.execut_db_sql(sql)
        self.log.info(drop_res)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, drop_res,
                      '执行失败：' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')